package com.yazi.mxz.behavior.quartz;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.Paging;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.SearchingFactory;
import com.yazi.mxz.behavior.common.ValueFactory;
import com.yazi.mxz.behavior.entity.beans.GoodsClassify;
import com.yazi.mxz.behavior.entity.enums.DataSyncType;
import com.yazi.mxz.behavior.entity.repository.DataSyncLog;
import com.yazi.mxz.behavior.repository.GoodsClassifyRepository;
import com.yazi.mxz.behavior.service.GoodsClassifyService;

/**
 * 商品类目数据同步(from oracle to mongodb)
 * @author mo_yq5
 * @date 2015-9-27
 */
public class GoodsClassifySyncQuartz extends SyncQuartzAbstract {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public static boolean reset = false;
	
	@Resource
	GoodsClassifyRepository goodsClassifyRepository;
	
	@Resource
	GoodsClassifyService goodsClassifyService;
	
	public boolean work() {
		if (isWorking) {
			logger.info("商品类目 sync working");
			return false;
		}
		
		if (reset) {
			reset = false;			
			logger.info("商品类目 del all: {}", goodsClassifyRepository.count());
			dataSyncLogRepository.delete(DataSyncType.GOODS_CLASSFIY.ordinal());
			goodsClassifyRepository.deleteAll();
		}
		
		logger.info("商品类目 sync start：{}", goodsClassifyRepository.count());
		
		isWorking = true;
		amount = 0;
		
		sycLog = dataSyncLogRepository.findOne(DataSyncType.GOODS_CLASSFIY.ordinal());
		if (null == sycLog) {
			sycLog = new DataSyncLog();
			sycLog.setId(DataSyncType.GOODS_CLASSFIY.ordinal());
			sycLog.setLastTime(-1L);
			dataSyncLogRepository.insert(sycLog);
		}
		
		// 最后更新的那条记录的时间作为下次同步的时间依据
		long latestTime = 0;
		
		Searching si = SearchingFactory.createSearching("updateTime", true, 1);
		
		Page<GoodsClassify> page;
		try {
			
			page = goodsClassifyService.listByGtUpdateTime(sycLog.getLastTime(), si);
			if (page.getPaging().getTotalResult() == 0) {
				latestTime = DateUtil.getTimeStamp();
				
			} else {
				
				latestTime = page.getList().get(0).getUpdateTime();
				si.setPageSize(Searching.MAX_PAGE_SIZE);
				checkRegion(si);
				
			}
			
			sycLog.setLastTime(latestTime);
			dataSyncLogRepository.save(sycLog);
			
			logger.info("商品类目 sync end: {}", amount);
			
		} catch (Exception e) {
			logger.error("商品类目同步异常：", e);
			return false;
		} finally {
			isWorking = false;			
		}
		
		return true;
	}
	
	private void checkRegion(Searching si) {
		
		Page<GoodsClassify> page = goodsClassifyService.listByGtUpdateTime(sycLog.getLastTime(), si);
		List<GoodsClassify> list = page.getList();
		GoodsClassify parent;
    	for (GoodsClassify classify : list) {
    		
    		logger.info("商品类目 sync: {},{}", classify.getClassifyId(), classify.getName());
    		
    		// goodsClassify for mongodb
    		com.yazi.mxz.behavior.entity.repository.GoodsClassify classify4mg = new com.yazi.mxz.behavior.entity.repository.GoodsClassify();
    		ValueFactory.combine(classify, classify4mg);

    		parent = classify.getGoodsClassify();
    		if (null != parent) {
    			classify4mg.setParentId(parent.getClassifyId());
    		}
    		
    		goodsClassifyRepository.save(classify4mg);
    		
		}

    	amount += list.size();
    	Paging pi = page.getPaging();
    	if (pi.getNowPage() < pi.getTotalPage()) {
    		si.setNowPage(pi.getNowPage() + 1);
    		checkRegion(si);
    	}
    	
	}
	
}
